package com.xyz.dao.logic;

import com.free.exception.SystemException;
import com.free.ext.mybatis.model.entity.PageResponseModel;
import com.free.utils.DateUtils;
import com.xyz.dao.dao.CCustomersignDao;
import com.xyz.domain.table.CCustomer;
import com.xyz.domain.table.CCustomersign;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class CCustomersignLogic {

    @Autowired
    private CCustomersignDao cCustomersignDao;

    public CCustomersign add(CCustomersign cCustomersign){
        return cCustomersignDao.insert(cCustomersign);
    }

    public Integer update(CCustomersign cCustomersign){
        return cCustomersignDao.update(cCustomersign);
    }

    public List<CCustomersign> list(CCustomersign cCustomersign, String orderBy){
        return cCustomersignDao.selectList(cCustomersign, orderBy);
    }

    public Integer delete(Integer key){
        return cCustomersignDao.delete(key);
    }

    public PageResponseModel<CCustomersign> pageInfo(CCustomersign cCustomersign, String orderBy, Integer pageNo, Integer pageSize){
        return cCustomersignDao.selectByPageNo(cCustomersign, orderBy, pageNo, pageSize);
    }

    public CCustomersign getTodaySignInfo(CCustomer customer) throws SystemException {
        Date currentDate = DateUtils.nowDate(DateUtils.FormatEnum.yyyy_MM_dd);
        CCustomersign cond = new CCustomersign();
        cond.setCusomterId(customer.getId());
        cond.setSigndate(currentDate);
        List<CCustomersign> signInfos = cCustomersignDao.selectList(cond, StringUtils.EMPTY);
        if(signInfos == null || signInfos.size() == 0) {
            return null;
        }
        return signInfos.get(0);
    }

    @Transactional
    public CCustomersign sign(CCustomer customer) throws SystemException {
        Date currentDate = DateUtils.nowDate(DateUtils.FormatEnum.yyyy_MM_dd);
        CCustomersign cond = new CCustomersign();
        cond.setCusomterId(customer.getId());
        cond.setSigndate(currentDate);
        List<CCustomersign> signInfos = cCustomersignDao.selectList(cond, StringUtils.EMPTY);
        if(signInfos == null || signInfos.size() == 0){
            cond.setCreatetime(new Date());
            return add(cond);
        }
        return null;
    }

    public Integer selectSignCount(CCustomersign sign){
        return cCustomersignDao.selectSingCnt(sign);
    }

    public Integer getMonthSignCount(int customerId, Date dateInfo){
        CCustomersign sign = new CCustomersign();
        String firstDay = DateUtils.convert2String(DateUtils.getMonthFirstDay(dateInfo), DateUtils.FormatEnum.yyyy_MM_dd);
        String lastDay = DateUtils.convert2String(DateUtils.getMonthFirstDay(dateInfo), DateUtils.FormatEnum.yyyy_MM_dd);
        sign.setStartDate(firstDay);
        sign.setEndDate(lastDay);
        sign.setCusomterId(customerId);
        return cCustomersignDao.selectSingCnt(sign);
    }

}